
/**
 * @Author: 陈瑞鹏
 * @Date:   2023-01-10 11:17:07
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-01-11 10:42:00
 */
import gameCtr from "../ctrs/gameCtr";
import netCtr from "../ctrs/netCtr";
import BaseGameLayer from "../libs/core/BaseGameLayer";
import { SceneManager } from "../libs/manager/SceneManager";
import rewardItem2 from "./rewardItem2";

const { ccclass, property } = cc._decorator;
@ccclass
export default class noticeLayer extends BaseGameLayer {

  @property({ type: cc.Label, tooltip: "标题" })
  titleTxt: cc.Label = null;

  @property({ type: cc.Label, tooltip: "内容" })
  descTxt: cc.Label = null;

  @property({ type: cc.Node, tooltip: "奖励节点" })
  rewardNode: cc.Node = null;

  @property({ type: cc.Node, tooltip: "返回节点" })
  backBtnNode: cc.Node = null;

  @property({ type: cc.Node, tooltip: "删除按钮" })
  delBtnNode: cc.Node = null;

  @property({ type: cc.Node, tooltip: "领取按钮" })
  lqBtnNode: cc.Node = null;

  onEnable(): void {
    super.onEnable();
  }

  protected onDataChanged(): void {
    super.onDataChanged();
    this.drawUI();
    console.log("onDataChanged")
  }

  drawUI() {
    this.titleTxt.string = this.customData.title;
    console.log(this.customData);
    if (this.customData.type == 1) {
      this.lqBtnNode.active = true;
      if (this.customData.isRecive) {
        this.lqBtnNode.getComponent(cc.Button).interactable = false;
      } else {
        this.lqBtnNode.getComponent(cc.Button).interactable = true;
      }
      this.delBtnNode.active = true;
      this.backBtnNode.active = false;
      this.descTxt.node.active = false;
      this.rewardNode.active = true;

      let arr = JSON.parse(this.customData.rewardArr);
      console.log(arr.length);
      let len = (arr.length * 100 + (arr.length - 1) * 10 - 100) / 2;
      arr.forEach((element, i) => {
        let node = gameCtr.ins.poolObj['rewardItem2'].get();
        node.x = i * 110 - len;
        console.log(i * 110 - len);
        this.rewardNode.addChild(node);
        node.getComponent(rewardItem2).drawUi(element);
      });

    } else if (this.customData.type == 2) {
      this.delBtnNode.active = false;
      this.lqBtnNode.active = false;
      this.backBtnNode.active = true;
      this.descTxt.node.active = true;
      this.rewardNode.active = false;
      this.descTxt.string = this.customData.desc;
    }
  }

  /**
   * 点击领取
   */
  onLqClick() {
    netCtr.ins.lqEamil(this.customData.uid).then(() => {
      this.lqBtnNode.getComponent(cc.Button).interactable = false;
    })
  }

  /**
   * 点击删除
   */
  onDelClick() {
    netCtr.ins.delEmail(this.customData.uid);
    SceneManager.getInstance().removeLayer(this.mediator.getMediatorName());
  }
}